/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kuti.grp5;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import kuti.EWorldElementType;
import kuti.IMagneticObstacle;
import kuti.IObservableMagneticObstacle;

/**
 * @author Marc Schorderet <marc.schorderet@edu.hefr.ch>
 * @author Vincenzo Do <vincenzo.do@edu.hefr.ch>
 * @version 1.0
 *
 * @see IMagneticObstacle
 */
public class MagneticObstacle implements IMagneticObstacle, IObservableMagneticObstacle, IRadarElement {

    /*
     * The segment that represents the obstacle
     */
    private Line2D segment;

    /**
     * 
     * @param segment the segment that represents the magnetic obstacle
     */
    public MagneticObstacle(Line2D segment) {
        if (segment == null) {
            throw new IllegalArgumentException("the obstacle is not defined");
        }

        if (Double.isNaN(segment.getP1().getX()) || Double.isNaN(segment.getP1().getY())) {
            throw new IllegalArgumentException("The start point of the obstacle is not a value");
        }
        if (Double.isNaN(segment.getP2().getX()) || Double.isNaN(segment.getP2().getY())) {
            throw new IllegalArgumentException("The start point of the obstacle is not a value");
        }

        this.segment = segment;
    }

    /**
     * 
     * @return the segment that represents the obstacle
     */
    @Override
    public Line2D segment() {
        return segment;
    }

    /**
     * 
     * @return the type of obstacle
     */
    @Override
    public EWorldElementType getElementType() {
        return EWorldElementType.MAGNETIC_OBSTACLE;
    }

    /**
     * This method returns if the magnetic obstacle is on the kuti trajectory.
     *
     * @param kuti kuti trajectory (or radar trajectory)
     *
     * @return Returns true if the magnetic obstacle intersects with the kuti
     * trajectory, else false.
     */
    @Override
    public boolean isIntersects(Line2D kuti) {
        return intersectsDistance(kuti) >= 0.0;
    }

    /**
     * This method returns the distance between the kuti center and the magnetic
     * obstacle.
     *
     * If there is no intersection point, this method returns a negative value.
     *
     * @param kuti kuti trajectory (or radar trajectory)
     *
     * @return Returns the distance between the kuti center and the magnetic
     * obstacle
     */
    @Override
    public double intersectsDistance(Line2D kuti) {
        if (kuti == null) {
            throw new IllegalArgumentException("The kuti line is not defined.");
        }
        if (Double.isNaN(kuti.getP1().getX()) || Double.isNaN(kuti.getP1().getY())) {
            throw new IllegalArgumentException("The start point of kuti line is not a number.");
        }
        if (Double.isNaN(kuti.getP2().getX()) || Double.isNaN(kuti.getP2().getY())) {
            throw new IllegalArgumentException("The end point of kuti line is not a number.");
        }

        Point2D iSegment = KutiTrajectory.intersectionLineLine(kuti, segment);

        double distance = Double.POSITIVE_INFINITY;
        boolean intersection = false;

        // Check the intersection with the segment
        if (iSegment != null) {
            distance = Math.min(distance, iSegment.distance(kuti.getP1()));
            intersection = true;
        }

        return (intersection) ? distance : Double.NEGATIVE_INFINITY;

    }
}
